﻿using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Image 类扩展
/// </summary>
public static class ImageExtension
{

    public static void SetActive(this Image image, bool isActive)
    {
        image.gameObject.SetActive(isActive);
    }

    public static void SetGray(this Image img, bool isGray) 
        => SetGray(img, isGray, null);

    public static void SetGray(this Image img, bool isGray, Material material)
    {
        if (isGray)
        {
            img.material = MaterialController.Instance.GetGray();
        }
        else
        {
            img.material = material;
        }
    }

    public static void SetGray(this Image img, bool isGray, bool withChild)
    {
        var imgs = img.GetComponentsInChildren<Image>(true);
        for (int i = 0, cnt = imgs.Length; i < cnt; i++)
        {
            imgs[i].SetGray(isGray);
        }
        var texts = img.GetComponentsInChildren<Text>(true);
        for (int i = 0, cnt = texts.Length; i < cnt; i++)
        {
            texts[i].SetGray(isGray);
        }
        var pss = img.GetComponentsInChildren<ParticleSystem>(true);
        for (int i = 0, cnt = pss.Length; i < cnt; i++)
        {
            pss[i].gameObject.SetActive(!isGray);
        }
    }

    public static void SetAlpha(this Image img, float alpha)
    {
        Color color = img.color;
        color.a = alpha;
        img.color = color;
    }

    public static void SetSprite(this Image image, string spriteName, bool isNativeSize = false, Action callback = null)
    {
        //TODO  设置图片
    }

    public static void SetColor(this Image image, string htmlStr)
    {
        ColorUtility.TryParseHtmlString(htmlStr, out var color);
        image.color = color;
    }

    public static void SetColor(this Image image, Color color)
    {
        image.color = color;
    }

    public static Coroutine SequenceFrame(this Image image, string[] picNames)
    {
        return image.StartCoroutine(Utility.Coroutine.StartSequence(image, picNames));
    }

}

